import { createApp } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import App from './App.vue'
import router from './router'
import './assets/main.css'

// qiankun微前端相关设置
import { renderWithQiankun, qiankunWindow } from 'vite-plugin-qiankun/dist/helper'
// 主题交由基座通过 microThemeBridge 控制，此处不再初始化 themeAdapter

let app: any

function render(props: any = {}) {
  const { container } = props
  // 创建Vue应用实例
  app = createApp(App)

  // 注册插件
  app.use(createPinia())
  app.use(router)
  app.use(ElementPlus, {
    locale: zhCn,
  })

  // 获取挂载容器
  const targetContainer = container ? container.querySelector('#app') : document.querySelector('#app')

  app.mount(targetContainer)
}

// 独立运行时
if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
  render()
}

// 实现qiankun生命周期钩子
renderWithQiankun({
  // 应用加载之前
  bootstrap() {
    console.log('[vue-app2] bootstrap')
    return Promise.resolve()
  },
  // 应用 render 之前触发
  mount(props) {
    console.log('[vue-app2] mount', props)
    render(props)
    
    // 主题由基座注入到 data-qiankun 容器的根CSS变量
    
    return Promise.resolve()
  },
  // 应用卸载之后触发
  unmount(props) {
    console.log('[vue-app2] unmount', props)
    
    // 主题适配器会自动清理
    
    app.unmount()
    app = null
    return Promise.resolve()
  },
  // 应用更新之前
  update(props) {
    console.log('[vue-app2] update', props)
    return Promise.resolve()
  }
}) 